package com.abou.mvc.controller.customer.product.search;

import org.apache.commons.lang.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.abou.constant.product.EClotheType;
import com.abou.constant.product.EProductType;
import com.abou.model.common.product.search.SearchProductsRequest;
import com.abou.model.common.product.search.SearchProductsResult;
import com.abou.mvc.controller.ControllerUtils;
import com.abou.mvc.controller.admin.HomeController;

/**
 * The controller for the search clothing products page
 * 
 * @author khalid
 */

public abstract class ACustomerClothesSearchController extends
    ACustomerSearchController {
  
  /**
   * The constructor
   * 
   * @param viewName The view name
   */
  public ACustomerClothesSearchController(String viewName,
      EProductType productType) {

    super(viewName, HomeController.VIEW_NAME, productType);
  }
  
  /**
   * Shows the home page.
   * 
   * @return The search result
   */
  @RequestMapping(method = RequestMethod.GET, params = "clotheType")
  @ModelAttribute(ControllerUtils.SEARCH_RESULT_ATT)
  public SearchProductsResult show(
      @RequestParam(required = false) String clotheType, ModelMap model) {

    SearchProductsRequest searchProductsRequest = createProductsRequest();
    
    if (!StringUtils.isBlank(clotheType)
        && EClotheType.valueOf(clotheType) != null) {
      
      searchProductsRequest.setClotheType(EClotheType.valueOf(clotheType));
    }
    
    model.put(ControllerUtils.SEARCH_REQUEST_ATT, searchProductsRequest);
    
    return getProducts(searchProductsRequest);
  }
  
  /**
   * @see com.abou.mvc.controller.customer.product.search.ACustomerSearchController
   *      #getProducts(com.abou.model.common.product.search.SearchProductsRequest)
   */
  @Override
  protected SearchProductsResult getProducts(SearchProductsRequest searchRequest) {

    SearchProductsResult searchResult = null;
    
    searchResult =
        getProductManager().listProductsClothes(searchRequest.getName(),
            searchRequest.getDescription(), searchRequest.getProductType(),
            searchRequest.getClotheType(), searchRequest.getSortOrder(),
            searchRequest.getPageIndex(), searchRequest.getRowsNbByPage());
    
    return searchResult;
  }
}
